var express = require('express');
var router = express.Router();
var sql = require('./../sql');
var Proall = require('./../sql/proall');

// router.post('/', (req, res) => {
//     sql.find(Proall, {}, { _id: 0 }).then(data => {
//         res.send(data)
//     })
// })

// 实现导入接口
router.post('/import', (req, res, next) => {
    // 1.获取信息
    let obj = json.parse()[0].data
    let arr = []
    // 2.遍历数据 --- 排除第一条数据
    obj.map((item, index) => {
        if (index !== 0) {
            arr.push({
                proid: "pro_" + uuid.v1(),
                type: item[0],
                brand: item[1],
                barndimg: item[2],
                proname: item[3],
                price: item[4],
                flag: item[5], // 是否推荐
                proimg: item[6],
                note: item[7],
                classification: item[8],
                title: item[9],
                classifyBean: [{
                    classifyCode: item[10],
                    classifyName: item[11],
                    picPath: item[12]
                }],
                classifySonBean: [{
                        classifyCode: item[13],
                        classifySonCode: item[14],
                        classifySonName: item[15],
                        picPath: item[16],
                        classifyThirdBean:[{
                                classifySonCode: item[17],
                                classifyThirdCode: item[18],
                                classifyThirdName: item[19],
                                picPath: item[20]
                        }]

                }]
            })
        }
    })
})

// 实现查询分类以及品牌的接口
router.post('/type', (req, res, next) => {
    // 1、获取字段名 类型 type 品牌 brand,查询数据库
    let { type } = res.body
    type = type || 'type'
    sql.distinct(Proall, type).then(data => {
      res.send({
        code: '200',
        success: '获取类型成功',
        // length: data.length,
        data: data
      })
    })
  })


// // 获取分类类型对应的品牌
router.get('/witchtype', (req, res, next) => {
    let { type } = res.body
    sql.find(Proall, { type }, {_id: 0,}).then(data => {
      // 数组去重 https://www.cnblogs.com/le220/p/9130656.html
      res.send({
        code: '200',
        message: '获取分类类型列表',
        data: data
      })
    })
  })

// 4、插入数据库
sql.insert(Proall, arr).then(() => {
    res.send(arr)
  })

module.exports = router
